import axios from 'axios';
import Vue from 'vue';

// 创建 axios 实例
const service = axios.create({
    baseURL: 'http://pnwl.cn:9000/lpro',
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
    },
});

// 请求拦截器
service.interceptors.request.use(
    config => {
        config.headers['User-Key'] = Vue.cookie.get('token');
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        if (response.data?.status !== 2) {
            if (Vue.prototype.$message) {
                // Vue.prototype.$message(response.data.statusMsg || 'Error,error');
            }
        } else {
            const responseData = response.data?.data?.data;
            console.log(responseData, 'success'); // 直接打印 responseData
            if (Array.isArray(responseData)) {
                response.data.data.data = responseData.map(it => {
                    it.atime = new Date(it.atime).toLocaleString();
                    it.mtime = new Date(it.mtime).toLocaleString();
                    if (it.mtime.includes('D')) {
                        it.mtime = '--';
                    }
                    return it;
                });
            } else {
                console.warn('responseData 不是一个数组', responseData);
            }
        }
        return response.data;
    },
    error => {
        console.log('error', error);
        return Promise.reject(error);
    }
);

export default service;
